<?php

namespace app\admin\controller;

use app\common\exception\BaseException;
use app\common\model\Goods as GoodsModel;
use app\common\controller\AdminController;
use app\common\service\annotation\ControllerAnnotation;
use think\App;

/**
 * @ControllerAnnotation(title="产品管理")
 */
class Goods extends AdminController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model           = new GoodsModel();
        $this->joinTable       = 'brand, spec, cate';
        $this->joinAddTable    = 'spec';
        $this->joinDelTable    = 'spec';
        $this->joinUpdateTable = 'spec';
        $this->each            = true;
        $this->fileName        = '标品导入模版';
        $this->defaultTemplate = [
                'name','code69','image','cate_id',
                'brand_id','min_unit_name','min_spec_name',
                'min_spec_sku','min_box_num','min_unit_convert','min_weight',
                'min_length','min_width','min_height',
                'max_unit_name','medium_unit_name','medium_spec_name',
                'medium_spec_sku','medium_box_num','medium_unit_convert','medium_weight',
                'medium_length','medium_width','medium_height',
                'max_unit_name','max_spec_name','max_spec_sku',
                'max_box_num','max_unit_convert','max_weight',
                'max_length','max_width','max_height'
        ];
        $this->importHeader    = [
                '商品名称*', '商品标品码', '商品主图','商品分类ID*',
                '商品品牌ID*', '小单位名称','规格名称(小)',
                '规格SKU(小)', '箱规数量(小)','规格转换(小)','重量(kg)(小)',
                '长度(cm)(小)', '宽度(cm)(小)', '高度(cm)(小)',
                '中单位名称','规格名称(中)', '规格SKU(中)',
                '箱规数量(中)','规格转换(中)','重量(kg)(中)',
                '长度(cm)(中)', '宽度(cm)(中)', '高度(cm)(中)',
                '大单位名称','规格名称(大)', '规格SKU(大)',
                '箱规数量(大)','规格转换(大)','重量(kg)(大)',
                '长度(cm)(大)', '宽度(cm)(大)', '高度(cm)(大)'
        ];
        $this->autoImport      = true;
    }

    public function each($list)
    {
        return $list->each(function ($item)
        {
            $item->brand = $item->brand()->field('id,name,logo')->find();
            $item->cate  = $item->cate()->field('id,name,icon')->find();
            $item->spec  = $item->spec()->select();
        });
    }

    /**
     * @throws BaseException
     */
    protected function actionImports(mixed $data)
    {

    }
}